package com.ihr360.binlog.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.ihr360.binlog.dao.BinLogReceivedEventRepository;
import com.ihr360.binlog.dto.BinLogReceivedEvent;
import com.ihr360.binlog.service.AggrateService;
import com.ihr360.binlog.service.BinLogReceiveEventService;
import com.ihr360.binlog.service.convertor.BinLogReceivedEventConvertor;
import com.ihr360.commons.entity.event.BinLogReceivedEventEntity;
import com.ihr360.commons.lang.CollectionUtils;
import com.ihr360.commons.model.type.BinLogEventStatus;
import com.ihr360.entity.SyncedRosterSalaryChangeRecordEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BinLogReceiveEventServiceImpl implements BinLogReceiveEventService {

    @Autowired
    private BinLogReceivedEventRepository binLogReceivedEventRepository;

    @Autowired
    private BinLogReceivedEventConvertor binLogReceivedEventConvertor;

    @Autowired
    private AggrateService aggrateService;


    @Override

    public BinLogReceivedEvent save(BinLogReceivedEvent event) {
        return binLogReceivedEventRepository.save(event);
    }

    @Override
    public void syncStaffSalaryProfile() {
        List<BinLogReceivedEventEntity> events = getStaffSalaryChangeEvent();
        if (CollectionUtils.isEmpty(events)) {
            return;
        }
        events.forEach(event -> {
            String content = event.getContent();
            SyncedRosterSalaryChangeRecordEntity entity = JSONObject.parseObject(content, SyncedRosterSalaryChangeRecordEntity.class);
            aggrateService.adjustStaffSalary(entity);
        });
        List<Long> eventIds = events.stream().map(BinLogReceivedEventEntity::getId).collect(Collectors.toList());
        finishStaffSalaryChangeEvent(eventIds);
    }


    @Override
    @Transactional
    public List<BinLogReceivedEventEntity> getStaffSalaryChangeEvent() {
        List<BinLogReceivedEvent> events = binLogReceivedEventRepository.findByTypeAndStatus(BinLogReceivedEvent.TYPE, BinLogEventStatus.PENDING);
        if (CollectionUtils.isEmpty(events)) {
            return Lists.newArrayList();
        }
        for (BinLogReceivedEvent event : events) {
            event.setStatus(BinLogEventStatus.IN_PROGRESS);
        }
        binLogReceivedEventRepository.save(events);
        return binLogReceivedEventConvertor.convertDtosToEntities(events);
    }

    @Override
    public void finishStaffSalaryChangeEvent(List<Long> eventIds) {
        if (CollectionUtils.isEmpty(eventIds)) {
            return;
        }
        List<BinLogReceivedEvent> events = (List<BinLogReceivedEvent>) binLogReceivedEventRepository.findAll(eventIds);
        if (CollectionUtils.isEmpty(events)) {
            return;
        }
        for (BinLogReceivedEvent event : events) {
            event.setStatus(BinLogEventStatus.FINISHED);
        }
        binLogReceivedEventRepository.save(events);
    }

}
